<?php
namespace app\controller;

use app\BaseController;
use app\Request;
use think\facade\Config;
use think\facade\Db;

class Index extends BaseController
{
    public function index()
    {
//        $data = array(
//            'a' => 10 ,
//            'b' => 20 ,
//            'c' => 30 ,
//            'd' => 40
//        );
//        $data = [10,20,30,40];
//        $sas = $this->randomSelect($data,50);

//        $data2 = [
//            [
//                'id' => 1,
//                'prize_name' => '超稀有奖品',
//                'rare' => 3,
//                'chance' => 1
//            ],
//            [
//                'id' => 2,
//                'prize_name' => '稀有奖品',
//                'rare' => 2,
//                'chance' => 19
//            ],
//            [
//                'id' => 3,
//                'prize_name' => '普通奖品',
//                'rare' => 1,
//                'chance' => 80
//            ],
//        ];
//
//        $sas2 = $this->randomSelect2($data2,50);
//        $gift = Db::name("scale_prize")
//            ->field('prize_name,rare')
//            ->find(1);
        $pwd_key = Config::get('app.pwd_key');
        print_r("<pre>");
        print_r($pwd_key);
        print_r("====2====");
        exit;
        return $sas2;
    }

    function randFloat($min=0, $max=100){
        $format_num = $min + mt_rand()/mt_getrandmax() * ($max-$min);
        return sprintf("%.3f",$format_num);
    }
    public function randomSelect2($array, $num, $numArr=[]){
        $datas = $array ;
        if( !is_array($datas) || count($datas) == 0 )
            return ;
        //根据字段last_name对数组$data进行降序排列
        $all_chances = array_column($datas,'chance');
        array_multisort($all_chances,SORT_ASC,$datas);
        $proSum = array_sum($all_chances);
        $random = rand(0,$proSum);
        $sum = 0 ;
        $flag = [];
        foreach($datas as $key => $val ){
            $sum += $val['chance'];
            if( $random <= $sum ){
                if(!isset($val['num'])){
                    $datas[$key]['num'] = 1;
                }else{
                    $datas[$key]['num']++;
                }
                $flag = $datas[$key];
                break ;
            }
        }
        $numArr[] = $flag;
        if($num > 1){
            $num--;
            return $this->randomSelect2($array,$num,$numArr);
        }else{
            $ss_str = "";
            //数组中所有值出现的次数
            $ss = array_count_values($numArr);

            foreach($ss as $k => $v){
                $ss_str .= $k." * ".$v."<br />";
            }
            return $ss_str;
        }
    }
    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
    public function vue(Request $request)
    {
        $id = $request->param('id');
        $name = $request->param('name');
        return 'ID:' .$id.'/'. $name;
    }
    public function randomSelect($array, $num, $numArr=[]){
        $datas = $array ;
        if( !is_array($datas) || count($datas) == 0 )
            return ;
        asort($datas); //按照大小排序
        $proSum = array_sum($datas);
        $random = rand(0,$proSum);
        $sum = 0 ;

        $flag = '';
        foreach($datas as $key => $data ){
            $sum += $data ;
            if( $random <= $sum ){
                $flag = $key;
                break ;
            }
        }
        $numArr[] = $flag;
        if($num > 1){
            $num--;
            return $this->randomSelect($array,$num,$numArr);
        }else{
            $ss_str = "";
            $ss = array_count_values($numArr);
            print_r("<pre>");
            print_r($ss);
            print_r("====2222====");
            exit;
            foreach($ss as $k => $v){
                $ss_str .= $k." * ".$v."<br />";
            }
            return $ss_str;
        }
    }
}
